package com.neuedu.jdbc;

import java.sql.*;
import java.util.TimeZone;

public class Demo01 {

    /***
     *  1 加载驱动
     *  2 创建链接
     *  3 创建Statemet
     *  4 执行sql
     *  5 解析结果集
     *  6 关闭资源
     * @param args
     */
    public static void main(String[] args)  {

        //驱动是 8.x
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        String sql = "  select count(1) from dept  ";

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //http://www.baidu.com/aaaa
            String url = "jdbc:mysql://127.0.0.1:3306/hrbu2021?useUnicode=true&serverTimezone=Asia/Shanghai&useSSL=false";
            String username = "root";
            String password = "root";
            conn = DriverManager.getConnection(url, username, password);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            //让rs指向数据行
            if(rs.next()){
                int count = rs.getInt(1);
                System.out.println("部门表的 count = " + count);
            }



        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }


}
